<?php

namespace Api\Library;

class Weixin
{
    public $access_token;

    public function __construct($access_token)
    {
        $this->access_token = $access_token;
    }

    /**
     * 根据  openid 发送模板信息给购买成功用户
     * @param $data
     * @return mixed
     */
    public function getTemplateMessage($data)
    {
        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->access_token;
        $res = $this->httpRequest($url, $data);
        return json_decode($res, true);
    }

    //http请求
    protected function httpRequest($url, $data = null)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

}